package com.JobFind.uploadfile.controller;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;

import com.JobFind.controller.BaseAction;
import com.opensymphony.xwork2.ActionSupport;



@Component
public class UploadFilesController extends ActionSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private File file;			//文件
	private String fileFileName;  //文件名 
	private String filePath;	  //文件路径
	private InputStream inputStream;
	private static String LOCALURl="http://localhost:8080/FindJob/upload/";
	
	public File getFile() {
		return file;
	}


	public void setFile(File file) {
		this.file = file;
	}


	public String getFileFileName() {
		return fileFileName;
	}


	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}


	public String getFilePath() {
		return filePath;
	}


	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}


	public InputStream getInputStream() {
		return inputStream;
	}


	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}


	public void uploadHeadPhoto () throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out =  response.getWriter();
		String path = ServletActionContext.getServletContext().getRealPath("/upload");
	    File file = new File(path); // 判断文件夹是否存在,如果不存在则创建文件夹
	    if (!file.exists()) {
	      file.mkdir();
	    }
	    try {
		      if (this.file != null) {
		      File f = this.getFile();
		      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		      String fileName = sdf.format(new Date()); // 采用时间方式命名
		      String name = fileName+"idphoto"+fileFileName.substring(fileFileName.lastIndexOf(".")); // 保存在硬盘中的文件名
	
		      FileInputStream inputStream = new FileInputStream(f);
		      FileOutputStream outputStream = new FileOutputStream(path+ "\\" + name);
		      byte[] buf = new byte[1024];
		      int length = 0;
		      while ((length = inputStream.read(buf)) != -1) {
		        outputStream.write(buf, 0, length);
		      }
		      outputStream.flush();
		      inputStream.close();
		      
		      //文件保存的完整路径  比如：D:\tomcat6\webapps\eserver\\upload\a0be14a1-f99e-4239-b54c-b37c3083134a.png
		      filePath = path+"\\"+name;
		      out.println(LOCALURl+name);
	      }
	    } catch (Exception e) {
	      e.printStackTrace();
	    }
	}
}
